#ifndef HTTP_HLS_CONNECTION_H_
#define HTTP_HLS_CONNECTION_H_

#include "public_module/hls_protocol/hls_m3u8_message.h"
#include "public_module/protocol_center/protocol_parser.h"

#include <functional>
#include <memory>
#include <string>
#include <vector>

class HttpConnection;
class Loop;
class HttpResponse;
class HttpHlsConnection
{
public:
    using OnMessageFn   = std::function<void(HlsM3U8Message * message)>;
    using OnMediaFileFn = std::function<void(const std::string & url, const uint8_t * data, int length)>;
    using OnDisConnfn   = std::function<void()>;

public:
    explicit HttpHlsConnection(Loop * loop);
    int  InitHlsUrl(const std::string & url);
    void SetOnMessageFn(OnMessageFn && fn);
    void SetMediaFileFn(OnMediaFileFn && fn);
    void SetOnDisconnfn(OnDisConnfn && fn);
    void CloseConnection() const;

private:
    void OnConnection(HttpConnection * connection, int result) const;
    void OnMessage(HttpConnection * connection, HttpResponse * response);
    void OnDisconn(HttpConnection * connection);

private:
    Loop *                          m_loop;
    std::shared_ptr<HttpConnection> m_http_conn;

private:
    std::string                  m_url;
    ProtocolParser::ProtocolData m_protocol_data{};
    bool                         m_state;

private:
    OnMessageFn   m_message_fn;
    OnMediaFileFn m_media_file_fn;
    OnDisConnfn   m_disconn_fn;
};

#endif
